package com.ssk.sh.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssk.sh.dao.ChatsMapper;
import com.ssk.sh.dto.ChatsDto;
import com.ssk.sh.mapper.ChatsModelMapper;
import com.ssk.sh.model.Chats;
import com.ssk.sh.service.IChatsService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-06-12
 */
@Service
@DS("chat")
@DSTransactional(rollbackFor = Exception.class)
public class ChatsServiceImpl extends ServiceImpl<ChatsMapper, Chats> implements IChatsService {

    @Override
    public Flux<PageInfo<ChatsDto>> page(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Chats> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Chats.ID);
        PageInfo<ChatsDto> pageInfo = new PageInfo<>(baseMapper.selectList(queryWrapper)
                .stream().map(ChatsModelMapper.INSTANCE::toDto).toList()
        );
        return Flux.just(pageInfo);

    }

    @Override
    public ChatsDto getChatsDto() {
        LambdaQueryWrapper<Chats> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chats::getWsId, 1)
                .orderByDesc(Chats::getWsId);
        Chats chats = baseMapper.selectList(queryWrapper).get(0);
        return chats == null ? null : ChatsModelMapper.INSTANCE.toDto(chats);
    }
}
